-- UPv0.3 Server
require "server"
math.randomseed(os.time())
mr=math.random(0,50000)
if not pcall(function() require "server_config" end) then 
	print("server config missing! defaulting password to ".."$"..mr.."!") 
	admin_password="$"..tostring(mr)
end
net.start(5137)
first=0
firstply=nil
secondply=nil
map=1
buttons={ }
authenticated={ }
print("UPv0.3 server initialized")
run=true
net.action("login",function(data,ply)
	if data==admin_password then
		authenticated[#authenticated+1]=ply
		print("authenticated server administration tool at address "..net.ip(ply))
		net.tell("-login-success",ply)
	else
		print("failed authentication attempt from "..net.ip(ply))
		net.tell("-login-false",ply)
	end
end)
net.event("close",function(ply) run=false end)
net.event("joined",function(ply)
	if first==0 then
		firstply=ply
		net.tell(":first",ply)
		print("player has been set to first.")
		net.action("state",1)
	else
		print("player has been set to second.")
		secondply=ply
		net.tell(":last",ply)
		net.action("state",2)
		net.action("mapid",map)
		net.action("update",function(data,ply)
			if ply~=secondply then return end
			net.tell("-update-"..data,firstply)
		end )
		net.action("fupdate",function(data,ply)
			if ply~=firstply then return end
			net.tell("-update-"..data,secondply)
		end )
	end
	first=1
end)
while (run) do
	net.process()
	if firstply and secondply then
	end
end